const { VM, VMScript } = require("vm2")
const fs = require("fs")
const path = require("path")
const user = require("./config/user.config.js")
const tools = require("./config/tools.config.js")
const env = require("./config/env.config.js")

const name = "test"

const configCode = fs.readFileSync(path.join(__dirname, "/config", "/config.js")) + "\r\n"

//功能插件相关函数
const toolsCode = tools.getCode()

//浏览器环境相关代码
const envCode = env.getCode()

//全局初始化代码
const globalVarCode = tools.getFile("globalVar")

//用户初始化代码
const userVarCode = user.getCode(name, "userVar")

//设置代理对象代码
const proxyObjCode = tools.getFile("proxyObj")

//需要调试的代码
const debugCode = user.getCode(name, "input")

//异步执行的代码
const asyncCode = user.getCode(name, "async")

// const code = fs.readFileSync(path.join(__dirname, "/run.js"))
// const code = `${toolsCode}${envCode}${globalVarCode}${userVarCode}${proxyObjCode}${debugCode}${asyncCode}`
const code = String.prototype.concat(configCode, toolsCode, envCode, globalVarCode, userVarCode, proxyObjCode, debugCode, asyncCode)



//测试运行代码
const script = new VMScript(code, "./debugJS.js")
// script.compile()
const vm = new VM()
const result = vm.run(script)


fs.writeFileSync(path.join(__dirname, `/user/${name}`, "/output.js"), code)


setInterval(() => {

}, 10000);


